package com.example.yiqiuser.controller;

import com.example.yiqiuser.entity.Tuser;
import com.example.yiqiuser.service.TuserService;
import com.example.yiqiuser.util.Page;
import com.example.yiqiuser.util.Result;
import com.example.yiqiuser.util.SmsUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @program: yiqiuser
 * @description:
 * @author: 诸葛龙辰
 * @create: 2023-04-05 20:37
 * @version:
 **/
@RestController
@RequestMapping("tuser")
public class TuserController extends BaseController {
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //依赖注入
    @Resource
    private TuserService tuserService;

    @Resource
    private HttpSession session;

    @Resource
    private HttpServletRequest httpServletRequest;

    private  String  verifyCode;

    private SmsUtil smsUtil =new SmsUtil();

    @PostMapping("queryPage")
    public Result queryPage(@RequestBody Page<Tuser> page) {
        return success(tuserService.queryPage(page));
    }

    @PostMapping("add")
    public Result add(@RequestParam("account") String acount, @RequestParam("password") String password, @RequestParam("name") String name
            , @RequestParam("smallname") String smallname, @RequestParam("sex") String sex, @RequestParam("mobile") String mobile) {
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());

        String format = simpleDateFormat.format(date);
        Tuser tuser1 = new Tuser();
        tuser1.setAccount(acount);
        tuser1.setPassword(password);
        tuser1.setName(name);
        tuser1.setSmallname(smallname);
        tuser1.setSex(sex);
        tuser1.setMobile(mobile);
        tuser1.setState(Integer.toString(1));
        tuser1.setCjtime(format);

        return success(tuserService.add(tuser1));

    }

    @PostMapping("update")
    public Result update(@RequestBody Tuser tuser) {
        return success(tuserService.update(tuser));
    }

    @DeleteMapping("delete")
    public Result delete(int id) {
        return success(tuserService.delete(id));
    }

    /**
     * 登录方法
     *
     * @param mobile
     * @param password
     * @return
     */
    @GetMapping("/login")
    public Result login(@RequestParam("mobile") String mobile, @RequestParam("password") String password) {
        Tuser tuser = new Tuser();
        tuser.setMobile(mobile);
        tuser.setPassword(password);
        Tuser userInfo = tuserService.checkTuer(tuser);
        if (userInfo != null) {
            session.setAttribute("userInfo", userInfo);
            return success(tuser);
        }
        return error("500");
    }
    /**
     * 订单判断信息
     * @param session
     * @return
     */
    /*@GetMapping("tick")
    public Result tick(HttpSession session){
        //获取登录游客信息
        Tuser userInfo = (Tuser)session.getAttribute("userInfo");

        return success(userInfo);
    }*/

    /**
     * 发送验证码
     * @param mobile
     * @return
     */
    @PostMapping("createCode")
    public  Result createVerifyCode(@RequestParam("mobile") String mobile){
        //生成随机验证码
        Random random =new Random();
        String num =(random.nextInt(900000)+100000)+"";
        //生成验证码session
        verifyCode=num;
    /*    System.out.println(verifyCode+"--------------------------------------------------------------------");
        System.out.println(mobile+"+++++++++++++++++++++++++++++++++++++++++++++++++++");*/
        //调用工具类中发送信息方法  需要参数手机号和验证码
        String isSuc = SmsUtil.SendSms(mobile, verifyCode);

        //如果验证码生成成功 返回success
        if (verifyCode!=null){
            return success(isSuc);
        }
        return error("500");
    }

    /**
     *
     * @param verifyCode1
     * @return
     */
    @PostMapping("verify")
    public Result verify(String verifyCode1){
        if (verifyCode1.equals(verifyCode)){
            return success("200");
        }else{
            return error("500");
        }
    }

    /**
     * 失去焦点验证手机号是否存在
     * @param mobile
     * @return
     */
    @PostMapping("checkMobile")
    public Result checkMobile(@RequestParam("mobile") String mobile){
        Tuser tuser = new Tuser();
        tuser.setMobile(mobile);

        Tuser tuser1 = tuserService.checkMobile(tuser);
        if (tuser1!=null){
            return  success(tuser1);
        }else{
            return error(500);
        }
    }

    /**
     * 订单判断信息
     * @param session
     * @return
     */
    @GetMapping("tick")
    public Result tick(HttpSession session){
        //获取登录游客信息
        Tuser tuser = (Tuser)session.getAttribute("userInfo");
        Result success = success(tuser);
        if (success.getData()==null){
            return error("500");
        }
        return success;
    }


}